package com.openapi.protocol.decode;

import com.openapi.data.JT808PackageData;
import com.openapi.protocol.decode.subPackageHandle.handle.FileStreamHandle;
import com.openapi.protocol.decode.subPackageHandle.handle.FileUploadFinishedHandle;
import com.openapi.protocol.decode.subPackageHandle.hj212Stream.FileInfoCache;
import exsun.jt808.data.config.jt808.Jt808MsgIdConfig;
import exsun.jt808.data.config.jt808.Jt808ReceiveMsgTopic;
import exsun.jt808.data.upload_data.Jt808EntityBase;
import exsun.jt808.data.upload_data.jsatl12.AlarmAttachmentInfo;
import exsun.jt808.data.upload_data.jsatl12.AttachmentFileInfo;
import exsun.jt808.data.upload_data.jsatl12.FileInfoUpload;
import exsun.jt808.data.util.parse.DecoderUtils;
import exsun.jt808.data.util.parse.HexStrUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 苏标协议解析
 */
@Component
public class AnalyzeHj212 {
	private final static Logger logger = LoggerFactory.getLogger(AnalyzeHj212.class);
	/**
	 * 苏标消息id
	 */
	private static ConcurrentHashMap<Integer, String> map = new ConcurrentHashMap<>();
	static{
		map.put(Jt808MsgIdConfig.jsalt12FileMsgId, Jt808ReceiveMsgTopic.fileStream);
		map.put(Jt808MsgIdConfig.alarmAttachmentInfo, Jt808ReceiveMsgTopic.alarmAttachmentInfo);
		map.put(Jt808MsgIdConfig.fileInfoUpload, Jt808ReceiveMsgTopic.fileInfoUpload);
		map.put(Jt808MsgIdConfig.fileUploadFinished, Jt808ReceiveMsgTopic.fileUploadFinished);
	}
	public static boolean bHj212Msg(Integer msgId){
		if(map.containsKey(msgId))return true;
		return false;
	}

	public static Jt808EntityBase fileStreamDecoder(JT808PackageData packageData){
		return FileStreamHandle.decode(packageData);
	}

	public static Jt808EntityBase fileUploadFinishedDecoder(JT808PackageData packageData){
		return FileUploadFinishedHandle.decode(packageData);
	}


	/**
	 * 报警附件信息消息
	 * @param packageData
	 * @return
	 */
	public static Jt808EntityBase alarmAttachmentInfoDecoder(JT808PackageData packageData) {
		byte[] body = packageData.getBodyBuffers();
		AlarmAttachmentInfo data = new AlarmAttachmentInfo();
		data.setDeviceID(DecoderUtils.parseStringFromBytes(body, 0, 7));
		data.setAlarmFlag(HexStrUtil.encodeHex(body, 7, 16));
		data.setAlarmCode(DecoderUtils.parseStringFromBytes(body, 23, 32).substring(5, 32));
		data.setType(DecoderUtils.parseIntFromBytes(body, 55, 1));
		data.setAttachmentsCount(DecoderUtils.parseIntFromBytes(body, 56, 1));
		List<AttachmentFileInfo> files = new ArrayList<AttachmentFileInfo>();
		int index = 57;
		for(int n = 0;n<data.getAttachmentsCount();n++)
		{
			AttachmentFileInfo fileInfo = new AttachmentFileInfo();
			int len = DecoderUtils.parseIntFromBytes(body, index, 1);
			index+=1;
			fileInfo.setFileName(DecoderUtils.parseStringFromBytes(body, index, len));
			index+=len;
			fileInfo.setFileSize(DecoderUtils.parseIntFromBytes(body, index, 4));
			index+=4;
			files.add(fileInfo);
		}
		for (AttachmentFileInfo file:files
		) {
			logger.info("文件列表信息：" + file.getFileName());
		}
		data.setFiles(files);

		FileInfoCache.updateFileInfo(packageData.getDvo(),data.getFiles(),
				data.getAlarmCode(),data.getAlarmFlag());

		return data;
	}

	/**
	 * 文件信息上传
	 * @param packageData
	 * @return
	 */
	public static Jt808EntityBase fileInfoUploadDecoder(JT808PackageData packageData) {
		byte[] body = packageData.getBodyBuffers();
		FileInfoUpload data = new FileInfoUpload();
		int len = DecoderUtils.parseIntFromBytes(body, 0, 1);
		data.setFileName(DecoderUtils.parseStringFromBytes(body, 1, len));
		data.setFileType(DecoderUtils.parseIntFromBytes(body, 1+len, 1));
		data.setFileSize(DecoderUtils.parseIntFromBytes(body, 2+ len, 4));
		logger.info("文件信息：" + data.getFileName());
		return data;
	}


}
